package com.example.app.pages.main.home.me

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.ArrowBack
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.example.app.barStateViewModel
import com.example.app.components.MyButton.MyFilledTonalButton
import com.example.app.components.MyField.MyField
import com.example.app.components.MyIcon.MyNavigationIcon
import com.example.app.components.MyImage.MyAsyncImage
import com.example.app.components.MyListItem.MyListItem
import com.example.app.components.MyScaffold.MyScaffoldHasTipAndLoading
import com.example.app.components.MySpacer.MySpacer
import com.example.app.components.MyText.MyText
import com.example.app.components.MyTopBar.MyTopBar
import com.example.app.pages.main.home.me.viewModel.AddNewViewModel
import com.example.app.routeViewModel
import com.example.app.ui.theme.listItemStartPadding
import com.example.app.ui.theme.middleSpacer
import com.example.app.util.CacheUtil
import com.example.app.viewmodel.BarStateViewModel
import com.example.app.viewmodel.MainPages
import com.example.app.viewmodel.MainViewModel
import com.example.app.viewmodel.RouteViewModel
import kotlinx.coroutines.CoroutineScope
import java.text.SimpleDateFormat

//我的
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Me(

) {
    var userInfo = CacheUtil.getUser()
    var curHomeChild = routeViewModel.curHomeChild.collectAsState()

    barStateViewModel.updateAll(
        isShowBottomBar = true,
        isStatusBarDarkIcon = true,
        statusBarColor = MaterialTheme.colorScheme.background,
        navigationBarColor = MaterialTheme.colorScheme.onPrimary,
        isNavigationBarDarkIcon = true
    )
    MyScaffoldHasTipAndLoading(
        topBar = {
            MyTopBar(
                title = { MyText(text = curHomeChild.value.title) },
                navigationIcon = {
                    MyNavigationIcon(
                        imageVector = Icons.Rounded.ArrowBack,
                        onClick = {
                        }
                    )
                })
        }) {
        if (userInfo != null) {
            Column(
                modifier = Modifier
                    .padding(it)
                    .fillMaxSize()
            ) {
                MySpacer(modifier = Modifier.height(middleSpacer))
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Center
                ) {
                    MyAsyncImage(
                        model = "https://img0.baidu.com/it/u=4129085291,1924555546&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500",
                        modifier = Modifier
                            .clip(CircleShape)
                            .size(50.dp)
                    )
                }
                MySpacer(modifier = Modifier.height(3.dp))
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.Center
                ) {
                    MyText(text = userInfo.Username!!)
                }
                MySpacer(modifier = Modifier.height(middleSpacer))

                MyListItem(
                    headlineContent = {
                        MyField(
                            label = "邮箱",
                            value = userInfo.Email!!
                        )
                    },
                    hasWrap = true
                )

                MySpacer(modifier = Modifier.height(12.dp))

                MyListItem(
                    headlineContent = {
                        MyField(
                            label = "生日",
                            value = SimpleDateFormat("YYYY-MM-dd").format(userInfo.CreateAt!!.toLong())
                        )
                    },
                    hasWrap = true
                )
                MySpacer(modifier = Modifier.height(12.dp))
                MyListItem(
                    headlineContent = {
                        MyField(
                            label = "地址",
                            value = userInfo.Position!!
                        )
                    },
                    hasWrap = true
                )


                MySpacer(modifier = Modifier.height(12.dp))

                MyFilledTonalButton(colors = ButtonDefaults.buttonColors(
                    containerColor = MaterialTheme.colorScheme.onPrimary,
                    contentColor = MaterialTheme.colorScheme.onPrimaryContainer
                ),
                    modifier = Modifier
                        .padding(
                            PaddingValues(
                                listItemStartPadding,
                                0.dp
                            )
                        )
                        .fillMaxWidth(),
                    onClick = {
                        CacheUtil.setUser(null)
                        routeViewModel.flowUpdate_curPage(MainPages.LOGIN)
                    }) {
                    MyText(
                        text = "退出登录",
                        style = MaterialTheme.typography.bodyMedium.copy(fontWeight = FontWeight.Medium)
                    )
                }
            }
        }
    }
}
